package com.pata.display
{
	import com.pata.enum.InvalidationType;
	import com.pata.skin.DefaultSkin;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * @data	2011-4-6	
	 * @author	pata
	 */
	public class BaseButton extends AbstractDisplay implements IDisplay
	{
		private var mLbl:Label;
		private var mBg:Sprite;
		private var mPrevBG:DisplayObject;
		
		private var mMouseStatus:String = "up";
		private var mSelected:Boolean = false;
		public function get selected():Boolean
		{
			return this.mSelected;
		}
		public function set selected(value:Boolean):void
		{
			if(mSelected === value) return;
			mSelected = value;
			if(mSelected)
			{
				setMouseState("disabled");
				this.enabled = false;
			}else
			{
				setMouseState("up");
				this.enabled = true;
			}
		}
		
		private var mEnabled:Boolean = true;
		public function get enabled():Boolean
		{
			return mEnabled;
		}
		public function set enabled(value:Boolean):void
		{
			if(mEnabled === value) return;
			mEnabled = value;
		}
		private var mLabel:String = "";
		public function get label():String
		{
			return mLabel;
		}
		public function set label(value:String):void
		{
			mLabel = value;
			this.invalidate( InvalidationType.DATA );
		}
		
		private var mLabelSize:int;
		public function set labelSize(value:int):void
		{
			mLabelSize = value;
			this.invalidate( InvalidationType.SIZE );
		}
		private var buttonSkin:Object = null;
		private var mUpSkin:DisplayObject = null;
		private var mOverSkin:DisplayObject = null;
		private var mDownSkin:DisplayObject = null;
		private var mDisabledSkin:DisplayObject = null;
		public function set upSkin(value:DisplayObject):void
		{
			mUpSkin = value;
			setButtonSkin()
			buttonSkin.upSkin = mUpSkin;
		}
		public function set overSkin(value:DisplayObject):void
		{
			mOverSkin = value;
			setButtonSkin()
			buttonSkin.overSkin = mOverSkin;
		}
		public function set downSkin(value:DisplayObject):void
		{
			mDownSkin = value;
			setButtonSkin()
			buttonSkin.downSkin = mDownSkin;
		}
		public function set disabledSkin(value:DisplayObject):void
		{
			mDisabledSkin = value;
			setButtonSkin()
			buttonSkin.disabledSkin = mDisabledSkin;
		}
		private function setButtonSkin():void
		{
			if(buttonSkin == null)
				buttonSkin = {};
		}
		public function BaseButton()
		{
			super( this );
		}
		override protected function configUI():void
		{
			addEventListener(MouseEvent.ROLL_OVER , mouseHandler, false, 0 ,true);
			addEventListener(MouseEvent.ROLL_OUT, mouseHandler, false, 0, true);
			addEventListener(MouseEvent.CLICK, mouseHandler, false, 0, true);
			
			buttonMode = true;
			mouseChildren = false;
			createBg();
			createLabel();
		}
		
		private function createBg():void
		{
			mBg = new Sprite();
			addChild(mBg);
		}
		
		private function refreshBg():void
		{
			//var styleName:String = (enabled) ? mMouseStatus : "disabled";
			var styleName:String = mMouseStatus;
			var background:DisplayObject;
			//使用默认Skin
			if(buttonSkin == null)
			{
							
				background = DefaultSkin.styles.buttonSkin[styleName + "Skin"];				
				
			}else
			{
				background = buttonSkin[styleName + "Skin"];
			}
			
			if(background != null)
			{
				mBg.addChildAt(background, 0);
				if(mPrevBG != null && mPrevBG != background){mBg.removeChild(mPrevBG); };
				mPrevBG = background;
			}
		}
		
		private function createLabel():void
		{
			mLbl = new Label();	
			addChild(mLbl);
			this.refreshBg();
		}
		
		private function mouseHandler(e:MouseEvent):void
		{
			if (e.type == MouseEvent.MOUSE_DOWN) {
				setMouseState("down");
			} else if (e.type == MouseEvent.ROLL_OVER) {
				setMouseState("over");
			} else if (e.type == MouseEvent.ROLL_OUT) {
				setMouseState("up");
			}
		}
		
		private function setMouseState(state:String):void
		{
			if (mMouseStatus === state) { return; }
			mMouseStatus = state;
			invalidate( InvalidationType.STATUS );
		}
		
		override public function draw():void
		{
			if(isInvalid( InvalidationType.STATUS ))
			{
				this.refreshBg();
			}
			if( isInvalid( InvalidationType.DATA))
			{
				mLbl.text = mLabel;
				mLbl.size = mLabelSize;
				mLbl.draw();
			}
			if( isInvalid( InvalidationType.SIZE))
			{
				drawLayout();
			}
			super.draw();
			
		}
		private function drawLayout():void
		{
			mBg.width = mWidth;
			mBg.height = mHeight;
			mLbl.move((mWidth - mLbl.width) / 2 ,(mHeight - mLbl.height) / 2);
		}
	}
}